import wx
from os import listdir
from os.path import isfile, join, splitext
from com.nasgektw.cards.client.model.Card import Card
from com.nasgektw.cards.client.gui.CardsListPanel import CardsListPanel
from com.nasgektw.cards.client.gui.ConfigureTeasePanel import ConfigureTeasePanel
from com.nasgektw.cards.client.model.Configuration import Configuration
def listFiles(directory):
    basedir = directory
    subdirlist = []
    files = []
    for item in listdir(directory):
        path = join(basedir, item)
        if isfile(path):
            if splitext(path)[1] in [".jpg", ".jpeg", ".png", ".bmp", ".gif"] :
                files.append(path)
        else:
            if not item in [".svn"] :
                subdirlist.append(path)
    for subdir in subdirlist:
        files += listFiles(subdir)
    return files
class WxUI(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.workspaceLabel = wx.TextCtrl(self, style=wx.TE_READONLY)
        self.workspaceSelectionButton = wx.Button(self, 1, "Choose workspace")
        Configuration.cards = []
        self.tabs = wx.Notebook(self, id = wx.ID_ANY, style = wx.BK_DEFAULT)
        self.cardListPanel = CardsListPanel(self.tabs)
        self.configureTeasePanel = ConfigureTeasePanel(self.tabs)
        self.tabs.AddPage(self.cardListPanel, "Card List")
        self.tabs.AddPage(self.configureTeasePanel, "Configure tease")
        self.__do_binding__()
        self.__do_layout__()
    def __on_select_workspace__(self, event):
        dialog = wx.DirDialog(None, "Choose a directory:", style = wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dialog.ShowModal() == wx.ID_OK:
            if self.workspaceLabel.GetValue() != dialog.GetPath():
                self.workspaceLabel.SetValue(dialog.GetPath())
                Configuration.workspace = dialog.GetPath()
                Configuration.cards = []
                for filename in listFiles(dialog.GetPath()):
                    card = Card(filename)
                    Configuration.cards.append(card)
                self.cardListPanel.refreshCards()
                self.configureTeasePanel.refreshCards()
        dialog.Destroy()
    def __do_binding__(self):
        self.workspaceSelectionButton.Bind(wx.EVT_BUTTON, self.__on_select_workspace__)
    def __do_layout__(self):
        self.SetSize((Configuration.defaultWidth, Configuration.defaultHeight))
        sizer = wx.GridBagSizer(5,5)
        sizer.Add(self.workspaceLabel, (0,0), wx.DefaultSpan, wx.EXPAND|wx.ALL, 0)
        sizer.Add(self.workspaceSelectionButton, (0,1), wx.DefaultSpan, wx.ALL, 0)
        sizer.Add(self.tabs, (1,0), (1,2), wx.EXPAND|wx.ALL, 0)
        sizer.AddGrowableCol(0, 1)
        sizer.AddGrowableRow(1, 1)
        self.SetSizer(sizer)
        self.Layout()